﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.Base;
using EasyXaf.BulkOperations;
using EasyXaf.Sample.Module.BusinessObjects;
using System.Diagnostics;

namespace EasyXaf.Sample.Module.Controllers;

public class UserViewController : ObjectViewController<ListView, ApplicationUser>
{
    public SimpleAction BatchAddUsersByXpoAction { get; }

    public SimpleAction BatchAddUsersBySqlSugarAction { get; }

    public SimpleAction BatchAddUsersAndGroupsBySqlSugarAction { get; }

    public UserViewController()
    {
        BatchAddUsersByXpoAction = new SimpleAction(this, "BatchAddUsersByXpoAction", PredefinedCategory.Edit)
        {
            Caption = "批量添加用户(XPO)"
        };

        BatchAddUsersBySqlSugarAction = new SimpleAction(this, "BatchAddUsersBySqlSugarAction", PredefinedCategory.Edit)
        {
            Caption = "批量添加用户(SqlSugar)"
        };

        BatchAddUsersAndGroupsBySqlSugarAction = new SimpleAction(this, "BatchAddUsersAndGroupsBySqlSugarAction", PredefinedCategory.Edit)
        {
            Caption = "批量添加用户与组(SqlSugar)"
        };
    }

    protected override void OnActivated()
    {
        base.OnActivated();

        BatchAddUsersByXpoAction.Execute += BatchAddUsersByXpoAction_Execute;
        BatchAddUsersBySqlSugarAction.Execute += BatchAddUsersBySqlSugarAction_Execute;
        BatchAddUsersAndGroupsBySqlSugarAction.Execute += BatchAddUsersAndGroupsBySqlSugarAction_Execute;
    }

    protected override void OnDeactivated()
    {
        BatchAddUsersByXpoAction.Execute -= BatchAddUsersByXpoAction_Execute;
        BatchAddUsersBySqlSugarAction.Execute -= BatchAddUsersBySqlSugarAction_Execute;
        BatchAddUsersAndGroupsBySqlSugarAction.Execute -= BatchAddUsersAndGroupsBySqlSugarAction_Execute;

        base.OnDeactivated();
    }

    private void BatchAddUsersByXpoAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        var sw = new Stopwatch();
        sw.Start();

        using var objectSpace = Application.CreateObjectSpace(typeof(ApplicationUser));
        var userGroup = objectSpace.FirstOrDefault<UserGroup>(g => g.Name == "UserGroup3");

        for (var i = 0; i < 10; i++)
        {
            for (var j = 0; j < 1000; j++)
            {
                var user = objectSpace.CreateObject<ApplicationUser>();
                user.UserName = $"Test-{DateTime.Now.Ticks}";
                user.IsActive = true;
                user.UserGroup = userGroup;
            }
            objectSpace.CommitChanges();
        }

        sw.Stop();

        ObjectSpace.Refresh();

        Application.ShowViewStrategy.ShowMessage($"XPO耗时：{sw.ElapsedMilliseconds}ms。");
    }

    private void BatchAddUsersBySqlSugarAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        var sw = new Stopwatch();
        sw.Start();

        if (ObjectSpace is XPObjectSpace objectSpace)
        {
            var userGroup = objectSpace.FirstOrDefault<UserGroup>(g => g.Name == "UserGroup3");

            objectSpace.BulkInsert<ApplicationUser>(Application, os =>
            {
                var cloneUserGroup = os.CloneObject(userGroup);
                for (var i = 0; i < 10000; i++)
                {
                    var user = os.CreateObject<ApplicationUser>();
                    user.UserName = $"Test-{DateTime.Now.Ticks}";
                    user.IsActive = true;
                    user.UserGroup = cloneUserGroup;
                }
            });
        }

        sw.Stop();

        ObjectSpace.Refresh();

        Application.ShowViewStrategy.ShowMessage($"SqlSugar耗时：{sw.ElapsedMilliseconds}ms。");
    }

    private void BatchAddUsersAndGroupsBySqlSugarAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        var sw = new Stopwatch();
        sw.Start();

        if (ObjectSpace is XPObjectSpace objectSpace)
        {
            objectSpace.BulkInsert(Application, new Type[] { typeof(ApplicationUser), typeof(UserGroup) }, os =>
            {
                for (var i = 0; i < 100; i++)
                {
                    var userGroup = os.CreateObject<UserGroup>();
                    userGroup.Name = $"UserGroup{i:00}-{DateTime.Now.Ticks}";

                    for (var j = 0; j < 100; j++)
                    {
                        var user = os.CreateObject<ApplicationUser>();
                        user.UserName = $"Test-{DateTime.Now.Ticks}";
                        user.IsActive = true;
                        user.UserGroup = userGroup;
                    }
                }
            });
        }

        sw.Stop();

        ObjectSpace.Refresh();

        Application.ShowViewStrategy.ShowMessage($"SqlSugar耗时：{sw.ElapsedMilliseconds}ms。");
    }
}
